package com.rapiddevsample;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.rapiddevsample.model.SampleData;
import com.v.rapiddev.base.AppManager;
import com.v.rapiddev.dialogs.core.MaterialDialog;
import com.v.rapiddev.dialogs.zdialog.OnDialogItemClickListener;
import com.v.rapiddev.dialogs.zdialog.ZDialog;
import com.v.rapiddev.http.okhttp3.zokhttp.OnZokHttpResponse;
import com.v.rapiddev.http.okhttp3.zokhttp.ZokHttp;
import com.v.rapiddev.utils.V;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ClassType:
 * User:wenyunzhao
 * ProjectName:RapidDevSample
 * Package_Name:com.rapiddevsample
 * Created on 2016-11-24 22:16
 */

public class HttpDemo extends BaseActivity {
    @Bind(R.id.post)
    Button post;
    @Bind(R.id.get)
    Button get;
    @Bind(R.id.tv)
    TextView tv;

    @Override
    public void init() {
        setContentView(R.layout.httpdemo);
        ButterKnife.bind(this);
    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {
        AppManager.getAppManager().finishActivity(this);
    }

    private MaterialDialog dialog;

    @OnClick({R.id.post, R.id.get})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.post:
                dialog = ZDialog.showZDlialog2(super.context, "提示", "正在请求……");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //延迟  展示进度条
                        doPost();
                    }
                }, 1000);
                break;
            case R.id.get:
                dialog = ZDialog.showZDlialog2(super.context, "提示", "正在请求……");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //延迟  展示进度条
                        doGet();
                    }
                }, 1000);
                break;
        }
    }

    private void doGet() {
        Map<String, String> map_get = new HashMap<>();
        map_get.put("a", "b");
        map_get.put("d", "2");
        ZokHttp.getInstance(60).doGet("http://www.baidu.com", map_get, new OnZokHttpResponse() {
            @Override
            public void OnSucc(String data) {
                V.w("App - OnSucc:" + data);
                dialog.dismiss();
                //模仿数据
                String sampleD = "{\"name\":\"张三\",\"sex\":\"男\"}";
                //解析到实体类
                SampleData sampleData = new Gson().fromJson(sampleD, SampleData.class);
                tv.append("\n解析成功：\n" + sampleData.toString());
            }

            @Override
            public void OnError(String errorMsg) {
                dialog.dismiss();
                V.w("App - OnError:" + errorMsg);
                tv.append("\n" + errorMsg);
            }
        }, "使用Get带参数请求百度");

    }

    private void doPost() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "b");
        map.put("d", "2");
        ZokHttp.getInstance(60).doPost("http://www.taobao.com", map, new OnZokHttpResponse() {
            @Override
            public void OnSucc(String data) {
                V.w("App - OnSucc:" + data);
                dialog.dismiss();
                dialog = ZDialog.showZDlialog(HttpDemo.super.context, "提示", "数据请求成功，是否解析", "解析", "不了", new OnDialogItemClickListener() {
                    @Override
                    public void onLeftButtonClickListener() {
                        //模仿数据
                        String sampleD = "{\"name\":\"张三\",\"sex\":\"男\"}";
                        //解析到实体类
                        SampleData sampleData = new Gson().fromJson(sampleD, SampleData.class);
                        tv.append("解析成功：\n" + sampleData.toString());
                    }

                    @Override
                    public void onRightButtonClickListener() {
                        //do something
                    }
                });
            }

            @Override
            public void OnError(final String errorMsg) {
                V.w("App - OnError:" + errorMsg);
                dialog.dismiss();
                tv.append("\n" + errorMsg);

            }
        }, "使用Post带参数请求百度");
    }
}
